<template>
  <div class="detail">
    <MesHeader title="缺料明细" icon="el-icon-s-grid"></MesHeader>
    <MesTable ref="detail" :tableData="tableData" :columns="columns"></MesTable>
  </div>
</template>

<script setup>
import MesTable from '@/components/basic/MesTable.vue'
import MesHeader from '@/components/basic/MesHeader.vue'
import { getCurrentInstance, ref } from 'vue'

defineEmits(['parentCall'])
const columns = [
  {
    title: '订单号',
    field: 'MoCode',
  },
  {
    title: '物料编号',
    field: 'InvCode',
  },
  {
    title: '物料名称',
    field: 'InvName',
  },
  {
    title: '物料规格',
    field: 'InvStandard',
  },
  // {
  //   title: '总缺料数',
  //   field: 'OrgQty',
  // },
  {
    title: '当前缺料数',
    field: 'Qty',
  },
  {
    title: '状态',
    field: 'Status',
    bind: {
      key: 'inStoreState',
      data: [],
    },
  },
  {
    title: '审核人',
    field: 'Verifier',
  },
  {
    title: '审核时间',
    field: 'VerifyDate',
  },
  {
    title: '创建时间',
    field: 'CreateDate'
  }
]
const currentRow = ref(null)
const detail = ref(null)
const that = getCurrentInstance().appContext.config.globalProperties
let tableData = ref([])
const rowClick = (row) => {
  console.log(row)
  currentRow.value = row
  getDetailData(row.Lack_Id)
}

const getDetailData = async (id) => {
  let param = {
    page: 1,
    rows: 100,
    sort: 'CreateDate',
    order: 'desc',
    wheres: '[]',
    value: id,
  }

  let res = await that.http.post(
    'api/Production_LackStock/getDetailPage',
    param,
    true
  )
  tableData.value = res.rows
}

//审核
const audit = async () => {
  let select = detail.value.getSelected()
  if (select.length === 0) return that.$message.error('请选择数据')
  let flag = true
  try {
    let list = select.map((item) => {
      if (item.Status == 1) {
        flag = false
        throw new Error('请选择未审核的数据')
      }
      let { LackList_Id, Qty, InvName } = item
      return {
        LackListId: LackList_Id,
        Qty,
        InvName,
      }
    })

    let param = {
      MoCode: select[0].MoCode,
      list,
    }

    if (flag) {
      let res = await that.http.post(
        '/api/Production_LackStock/Verify',
        param,
        true
      )
      if (res.status) {
        that.$message.success('审核成功')
        rowClick(currentRow.value)
      } else {
        that.$message.error(res.message)
      }
    }
  } catch (e) {
    that.$message.warning(e.message)
  }
}

defineExpose({ rowClick, audit })
</script>

<style lang="less" scoped>
.detail {
  padding: 15px;
}
</style>
